package com.guigu.qingcheng.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.orderservice.mapper.TbOrderLogMapper;
import com.guigu.qingcheng.orderservice.service.TbOrderLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.order.TbOrderLog;
import com.guigu.qingcheng.utils.R;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbOrderLogServiceImpl extends ServiceImpl<TbOrderLogMapper, TbOrderLog> implements TbOrderLogService {

    @Override
    public R findPage(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbOrderLog> tbOrderLogPage = new Page<>(page,size);

        QueryWrapper<TbOrderLog> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wrapper.like("name",searchMap.get("name"));
        }
        baseMapper.selectPage(tbOrderLogPage,wrapper);
        return R.ok().data("total",tbOrderLogPage.getTotal()).data("rows",tbOrderLogPage.getRecords());
    }

    @Override
    public R findByOrderId(String id) {
        QueryWrapper<TbOrderLog> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",id);
        List<TbOrderLog> list = baseMapper.selectList(wrapper);
        return R.ok().data("log",list);

    }

    @Override
    public R findByOrderTime(String id, String status) {
        QueryWrapper<TbOrderLog> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",id);
        wrapper.eq("order_status",status);
        List<TbOrderLog> list = baseMapper.selectList(wrapper);
        if (list.size()>0) {
            return R.ok().data("log",list.get(0));
        }
        return R.error();
    }


}
